package com.ct.gd.passport.view.consult;

import java.util.Arrays;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.ct.gd.passport.R;
import com.ct.gd.passport.domain.AdviceDetail;
import com.up91.common.android.content.SimpleLoader;
import com.up91.common.android.view.adapter.SimpleListAdapter;

public class ConsultHistoryFragment extends Fragment implements LoaderCallbacks<List<AdviceDetail>>{
	private ListView				mAreaHistory;
	private ConsultHistroyAdapter	mAdapter;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.consult_history, container, false);
		mAreaHistory = (ListView) v.findViewById(R.id.area_advice_history);
		mAdapter = new ConsultHistroyAdapter(getActivity(), null);
		mAreaHistory.setAdapter(mAdapter);
		return v;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		load();
	}

	private class ConsultHistroyAdapter extends SimpleListAdapter<AdviceDetail> {
		private class ViewHolder {
			TextView tvSubmitTime;
			TextView tvSubmitContent;
		}
		public ConsultHistroyAdapter(Context context, List<AdviceDetail> data) {
			super(context, data);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if(null == convertView) {
				convertView = mInflater.inflate(R.layout.consult_history_list_item, parent, false);
				holder = new ViewHolder();
				holder.tvSubmitTime = (TextView) convertView.findViewById(R.id.tv_consult_history_submit_time);
				holder.tvSubmitContent = (TextView) convertView.findViewById(R.id.tv_consult_history_submit_content);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			AdviceDetail adviceDetail = getData().get(position);
			holder.tvSubmitTime.setText(adviceDetail.getSubmitTime());
			holder.tvSubmitContent.setText(adviceDetail.getSubmitContent());
			return convertView;
		}
	}

	private void load() {
		AdviceDetailsLoader loader = getLoader();
		loader.reset();
		loader.startLoading();
	}
	
	private AdviceDetailsLoader getLoader() {
		LoaderManager loaderManager = getActivity().getSupportLoaderManager();
		Loader<List<AdviceDetail>> loader = loaderManager.getLoader(0);
		if (null == loader) loader = loaderManager.initLoader(0, null, this);
		return (AdviceDetailsLoader) loader;
	}
	
	private static class AdviceDetailsLoader extends SimpleLoader<List<AdviceDetail>> {
		public AdviceDetailsLoader(Context context, View view) {
			super(context, view);
		}

		@Override
		protected List<AdviceDetail> loadData() throws Exception {
			//TODO
			return Arrays.asList(AdviceDetail.load(0));
		}
	}
	
	@Override
	public Loader<List<AdviceDetail>> onCreateLoader(int arg0, Bundle arg1) {
		return new AdviceDetailsLoader(getActivity(), mAreaHistory);
	}

	@Override
	public void onLoadFinished(Loader<List<AdviceDetail>> arg0, List<AdviceDetail> arg1) {
		if(null != arg1) {
			mAdapter.setData(arg1);
			mAdapter.notifyDataSetChanged();
		}
	}

	@Override
	public void onLoaderReset(Loader<List<AdviceDetail>> arg0) {
	}

}
